//
//  LBTexture.h
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 11/12/12.
//  Copyright (c) 2012 UIT. All rights reserved.
//

#ifndef OpenGLGame_LBTexture_h
#define OpenGLGame_LBTexture_h

class LBProgram;
class Matrix;

class LBTexture {

protected:
    GLuint texId;
    float width;
    float height;
    int textureActive;
    int byteInAPixel;
    float angle;
    float xScale, yScale;
    
    float sX, sY, sWidth, sHeight;          //source rectangel
    float dX, dY, dWidth, dHeight;          //destination rectangle
    GLfloat imageVertices[12];
    GLfloat texCoors[8];
    int xCenter, yCenter;                 // rotate center, 0, 0 is left-bottom corner, x to right, y to down.
    
    GLuint positionAttribute;
    GLuint texCoorAttribute;
    GLuint textureUniform;
    GLuint mWorldUniform;
    GLuint mProjectionUniform;
    
    bool isUseDefaultUniform;
    bool isDeleteTexIdWhenDeleteObj;
    
    Matrix *mWorld;
    
    virtual void calculateVertices();
    
public:
    LBTexture();
    LBTexture(int width, int height);
    LBTexture(const unsigned char* imageData, int width, int height);
    LBTexture(const unsigned char* imageData, int width, int height, int bytePerPixel);
    LBTexture(unsigned int texId, int width, int height);
    ~LBTexture();
    
    void sourceRect(float x, float y, float width, float height);
    void resetSourceRect();
    
    void render();
    void renderUseDefaultProgram();
    void render(LBProgram* program);
    virtual void calculateMatrix();
    
    //getter and setter
    GLuint getTexId();
    GLfloat* getImageVertices();
    GLfloat* getTexCoors();
    float getWidth();
    float getSourceHeight();
    float getSourceWidth();
    float getHeight();
    int getByteInAPixel();
    Matrix* getWorldMatrix();
    
    void setLocationAndScale(float x, float y, float scaleX, float scaleY);
    void setLocation(float x, float y);
    void setLocationByCenter(float xCenter, float yCenter);
    void setTextureActive(int textureActive);
    void setUniformAndAttribute(GLuint positionAttribute, GLuint texCoorAttribute, GLuint textureUniform);
    void setAngle(float angle);
    void setScale(float xScale, float yScale);
    void setIsDeleteTexIdWhenDeleteObj(bool isDeleteTexIdWhenDeleteObj);
};

#endif
